<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Exception;

class UserController extends Controller
{
    //用户列表
    public function lists(Request $request){
    	$input = $request->input();

    	$where = [];

    	$user_name = '';
    	if(!empty($input['users_name'])){
            $user_name = $input['users_name'];
    		$where[] = ['email','=',$input['users_name']];
    	}
    	$users = Db::table('users')->where($where)->orderBy('id','desc')->paginate(15);

    	return view('users',['users'=>$users,'users_name'=>$user_name]);
    }

    //添加用户
    public function add_user(){

    	return view('user_add');
    }

    //编辑用户
    public function edit_user(Request $request,$id){
    	$users = Db::table('users')->where('id','=',$id)->first();


    	return view('user_edit',['users'=>$users]);
    }

    //添加用户动作
    public function add_user_do(Request $request){
    	$input = $request->input();

    	unset($input['sure_password']);

    	$users = Db::table('users')->where('email','=',$input['email'])->first();
    	if(!empty($users->id) && (empty($input['id']))){
    		throw new Exception("该账号名已注册过了！");
    	}

    	if(!empty($input['password'])){
    		$input['password'] = Hash::make($input['password']);
    	}else{
    		unset($input['password']);
    	}

    	if(empty($input['id'])){
    		$id = Db::table('users')->insertGetId($input);
            Db::table('menu_permission')->insert(['uid'=>$id,'menu_id'=>json_encode([])]);
    	}else{
    		Db::table('users')->where('id','=',$input['id'])->update($input);
    	}

    	return response()->json([
            'status' => 1,
            'result' => [],
            'message' => '添加成功！'
        ]);
    }

    //删除用户
    public function del_user(Request $request,$id){
        Db::table('menu_permission')->where('uid','=',$id)->delete();
        Db::table('users')->where('id','=',$id)->delete();

        return response()->json([
            'status' => 1,
            'result' => [],
            'message' => '添加成功！'
        ]);
    }

    //栏目权限
    public function menu_user_left(Request $request,$id){
    	$users = Db::table('menu_permission')->where('uid','=',$id)->first();

    	//左栏目
    	$menus = Db::table('menu')->where("is_display",'=','1')->get();

    	//是否已选中
    	if(!empty($users->menu_id)){
    		$is_menu = json_decode($users->menu_id,1);
    	}else{
    		$is_menu = [];
    	}

    	foreach ($menus as $key => $value) {
    		if(in_array($value->id,$is_menu)){
    			$value->is_selected = 1;
    		}else{
    			$value->is_selected = 0;
    		}
    	}

    	return view('menu_user_left',['menus'=>$menus,'user_id'=>$id]);
    }

    //添加栏目权限
    public function add_menu_user(Request $request){
        $input = $request->input();
        $input['id'] = explode(",", $input['id']);
        $menu_permission_result = Db::table('menu_permission')->where('uid','=',$input['user_id'])->first();

        if(empty($menu_permission_result->uid)){
            $menu['uid'] = $input['user_id'];
            $menu['menu_id'] = json_encode($input['id']);
            Db::table('menu_permission')->insert($menu);
        }else{
            Db::table('menu_permission')->where('uid','=',$input['user_id'])->update(['menu_id'=>json_encode($input['id'])]);
        }

        return response()->json([
            'status' => 1,
            'result' => [],
            'message' => '添加成功！'
        ]);
    }
}
